using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using UnityEngine;

public class AndroidPurchases : ISerializable
{
	private static string publickey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvEC6GI9Yz/SO9Hj/bdFFUtHpr8RnaDQu2tcvP73Yy7zq9S3psZQet9qOJcazXcBsOzJCZbbHqIhdOAHdUQ+K4JXLgrzx6te4teFC+a9zTfiYyBxV2i6V1nqDd/x+vc7HoVYpE08IFDdP9lKCGy1fGcas5CZZiI4S1+Kzs71+6tIXrXYnXL2z+oHguynoxlc5QZqxk5YB/3kVN/yeWi0iQfbGsmQUQlxvVfNxGMS1U8Pr5IVKraGvEvzDNwdZCvmVSpISv0cW/S9NNNyQrMXI1II0HE4/WFSeoF5ts729k5GVoYssMgIRIveL9dgMS3yzHcJ90zsLQLqS/8Aj0DlFqwIDAQAB";

	private static string[] purchasableitems = new string[1]
	{
		"spintops.full"
	};

	private static string purchasefilename = "utilityfile.spintops";

	private static AndroidPurchases instance;

	private List<PurchaseInfo> _purchaseinfos;

	private static bool started = false;

	private static UnityGameObjectReceiver unitygameobjectreceiver;

	public static AndroidPurchases Instance
	{
		get
		{
			if (instance == null)
			{
				instance = new AndroidPurchases();
			}
			return instance;
		}
	}

	public static string[] PurchasableItems => purchasableitems;

	public List<PurchaseInfo> PurchaseInfos
	{
		get
		{
			return _purchaseinfos;
		}
		set
		{
			_purchaseinfos = value;
		}
	}

	public AndroidPurchases()
	{
		_purchaseinfos = new List<PurchaseInfo>();
		unitygameobjectreceiver = GameObject.Find("UnityGameObjectReceiver").GetComponent<UnityGameObjectReceiver>();
		started = false;
	}

	public static void UpdatePurchase(PurchaseInfo purchaseInfo)
	{
		if (purchaseInfo == null)
		{
			return;
		}
		UnityEngine.Debug.Log("OrderID:" + purchaseInfo.orderId + " productId:" + purchaseInfo.productId + " purchaseState:" + purchaseInfo.purchaseState);
		if (purchaseInfo.purchaseState == "PURCHASED" && !Instance.PurchaseInfos.Contains(purchaseInfo))
		{
			Instance.PurchaseInfos.Add(purchaseInfo);
			if (purchaseInfo.productId == "spintops.full")
			{
				PlayerPrefs.SetInt("tmp", 7);
			}
			Save();
		}
	}

	public static bool HasPurchased()
	{
		return Instance.PurchaseInfos.Count != 0;
	}

	public static bool HasPurchased(string productid)
	{
		foreach (PurchaseInfo purchaseInfo in Instance.PurchaseInfos)
		{
			if (purchaseInfo.productId == productid)
			{
				return true;
			}
		}
		return false;
	}

	public static void Load()
	{
		UnityEngine.Debug.Log("Loading Purchases.");
		string filename = Application.persistentDataPath + "/" + purchasefilename;
		instance = DeSerializObject<AndroidPurchases>(filename);
		if (instance == null)
		{
			RestoreTransactions();
		}
		foreach (PurchaseInfo purchaseInfo in Instance.PurchaseInfos)
		{
			UnityEngine.Debug.Log("Purchased: " + purchaseInfo.productId);
			if (purchaseInfo.productId == "spintops.full")
			{
				PlayerPrefs.SetInt("tmp", 7);
			}
		}
	}

	public static void LoadStart()
	{
		UnityEngine.Debug.Log("Loading Purchases.");
		string filename = Application.persistentDataPath + "/" + purchasefilename;
		instance = DeSerializObject<AndroidPurchases>(filename);
		if (instance == null)
		{
			PlayerPrefs.DeleteKey("tmp");
		}
		foreach (PurchaseInfo purchaseInfo in Instance.PurchaseInfos)
		{
			UnityEngine.Debug.Log("Purchased: " + purchaseInfo.productId);
			if (purchaseInfo.productId == "spintops.full")
			{
				PlayerPrefs.SetInt("tmp", 7);
			}
		}
	}

	private static void Save()
	{
		UnityEngine.Debug.Log("Saving Purchases.");
		string text = Application.persistentDataPath + "/" + purchasefilename;
		UnityEngine.Debug.Log(text);
		SerializeObject(text, Instance);
	}

	public static void SerializeObject<T>(string filename, T data)
	{
		XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
		TextWriter textWriter = new StreamWriter(filename);
		xmlSerializer.Serialize(textWriter, data);
		textWriter.Close();
	}

	public static T DeSerializObject<T>(string filename)
	{
		try
		{
			using (Stream stream = new FileStream(filename, FileMode.Open))
			{
				XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
				T result = (T)xmlSerializer.Deserialize(stream);
				stream.Close();
				return result;
			}
		}
		catch
		{
			return default(T);
		}
	}

	public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
	{
	}

	public static void StartInAppBilling()
	{
		if (!started)
		{
			started = true;
			UnityEngine.Debug.Log("StartInAppBilling");
			AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.platoevolved.inappbilling.UnityAndroidInterface");
			androidJavaClass.CallStatic("StartInAppBilling", publickey);
		}
	}

	public static void StopInAppBilling()
	{
		if (started)
		{
			started = false;
			UnityEngine.Debug.Log("StopInAppBilling");
			try
			{
				AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.platoevolved.inappbilling.UnityAndroidInterface");
				androidJavaClass.CallStatic("StopInAppBilling");
			}
			catch (Exception ex)
			{
				UnityEngine.Debug.Log(ex.Message);
			}
		}
	}

	public static void PurchaseItem(string productID)
	{
		StartInAppBilling();
		UnityEngine.Debug.Log("ANDROID - PurchaseItem");
		AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.platoevolved.inappbilling.UnityAndroidInterface");
		androidJavaClass.CallStatic("PurchaseItem", productID);
	}

	public static void RestoreTransactions()
	{
		StartInAppBilling();
		UnityEngine.Debug.Log("ANDROID - RestoreTransactions");
		AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.platoevolved.inappbilling.UnityAndroidInterface");
		androidJavaClass.CallStatic("RestoreTransactions");
	}

	public static string LogInfo()
	{
		return unitygameobjectreceiver.logInfo;
	}
}
